package com.health.work.activity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.health.work.R;
import com.health.work.R.id;
import com.health.work.utils.DefaultJSONData;
import com.health.work.utils.RequestParamtersUtil;
import com.health.work.utils.TipsUtil;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * 更改密码页面
 * 
 * @author star
 * 
 */
public class ChangePasswordActivity extends Activity {

	private Button back;
	private Button save;
	private EditText oldPassword;
	private EditText newPassword;
	private EditText newPasswordAgain;
	private String oldpassword;
	private String newpassword;
	private String passwordagain;
	private String phoneNum;

	private final static int SUCCESS = 0;
	private final static int FAILE = 1;
	private final static int ERROR = 2;
	private Thread thread;
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case SUCCESS:
				TipsUtil.showAlertFinish(ChangePasswordActivity.this, null,
						"密码修改成功！请重新登录");
				break;
			case FAILE:
				TipsUtil.showAlert(ChangePasswordActivity.this, null, "密码修改失败！");
				break;
			case ERROR:
				TipsUtil.showAlert(ChangePasswordActivity.this, null, "未知错误！");
				break;
			default:
				break;
			}

		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.change_password_layout);
		initPages();
	}

	private void initPages() {
		Bundle bundle = getIntent().getExtras();
		if (bundle != null) {
			phoneNum = (String) bundle.getString("phoneNum");
		}
		back = (Button) findViewById(R.id.btnBack);
		save = (Button) findViewById(R.id.save);
		oldPassword = (EditText) findViewById(R.id.old_password);
		oldPassword.setVisibility(View.GONE);
		newPassword = (EditText) findViewById(R.id.new_password);
		newPasswordAgain = (EditText) findViewById(id.new_password_again);
		back.setOnClickListener(onClickListener);
		save.setOnClickListener(onClickListener);
	}

	OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent i;
			switch (v.getId()) {
			case R.id.btnBack:
				// finish();
				i = new Intent(ChangePasswordActivity.this,
						HealthPassportActivity.class);
				startActivity(i);
				break;
			case R.id.save:
				submit();
				break;
			default:
				break;
			}
		}
	};

	/**
	 * 保存信息
	 */
	private void submit() {

		oldpassword = "123456";
		newpassword = newPassword.getText().toString();
		passwordagain = newPasswordAgain.getText().toString();
		if ("".equals(oldpassword)) {
			Toast.makeText(ChangePasswordActivity.this, "请输入旧密码",
					Toast.LENGTH_SHORT).show();
		} else if ("".equals(newpassword)) {
			Toast.makeText(ChangePasswordActivity.this, "请输入新密码",
					Toast.LENGTH_SHORT).show();
		} else if ("".equals(passwordagain)) {
			Toast.makeText(ChangePasswordActivity.this, "请再次确认新密码",
					Toast.LENGTH_SHORT).show();
		} else if (!newpassword.equals(passwordagain)) {
			Toast.makeText(ChangePasswordActivity.this, "两次密码输入不一样",
					Toast.LENGTH_SHORT).show();
		} else if (newpassword == oldpassword) {
			Toast.makeText(ChangePasswordActivity.this, "新密码不能与旧密码相同",
					Toast.LENGTH_SHORT).show();
		} else if (phoneNum != null) {
			getData();

		}
	}

	/**
	 * 请求网络数据
	 */
	private void getData() {
		thread = new Thread(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				new RequestParamtersUtil().UpdatePassWord(phoneNum,
						newpassword, new DefaultJSONData() {
							@Override
							public void parse(String result)
									throws JSONException {
								if ("True".equals(result)) {
									handler.sendEmptyMessage(SUCCESS);
								} else if ("False".equals(result)) {
									handler.sendEmptyMessage(FAILE);
								} else {
									handler.sendEmptyMessage(ERROR);
								}
							}

							@Override
							public void parse(JSONObject object)
									throws JSONException {
							}

							@Override
							public void parse(JSONArray array)
									throws JSONException {
							}
						});

			}
		});
		thread.start();

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onRestart() {
		super.onRestart();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return super.onKeyDown(keyCode, event);
	}
}
